import App from './App'
import router from '@/common/router'
import store from '@/common/store'
import { RouterMount } from 'uni-simple-router'
// 国际化 json 文件，文件内容详见下面的示例
import messages from './locale/index'

const {
	httpGet,
	httpPost,
	isLogin,
	msg,
	ZC,
	timeToStr,
	appLogin,
	app_config,
	getQueryString,
	getHashString,
	checkLogin,
	alertLogin,
	numConvert
} = require("./utils/common.js")
Vue.prototype.$httpGet = httpGet
Vue.prototype.$httpPost = httpPost
Vue.prototype.$isLogin = isLogin
Vue.prototype.$appLogin = appLogin
Vue.prototype.$msg = msg
Vue.prototype.$ZC = ZC
Vue.prototype.$timeToStr = timeToStr
Vue.prototype.$isDev = app_config.is_dev
Vue.prototype.$app_config = app_config
Vue.prototype.$checkLogin = checkLogin
Vue.prototype.$alertLogin = alertLogin
Vue.prototype.$numConvert = numConvert

var token = getQueryString('token') || getHashString('token')
if (token) {
	var old_token = ZC('user_token')
	if (token != old_token) {
		ZC.clear('user_info')
		ZC('user_token', token, -1)
	}
	location.href = location.origin + location.pathname + location.search.replace(/[?&]token=[^&]*&?/g, "") + "#" + location.hash.replace(/[?&]token=[^&]*&?/g, "")
	// console.log(location.origin + location.pathname + location.search.replace(/[?&]token=[^&]*&?/g, "") + "#" + location.hash.replace(/[?&]token=[^&]*&?/g, ""))
}


uni.navigateTo = (option)=>{
	var page = getCurrentPages()[0]
	page.$Router.push({
	    path: option.url,
		query: option.data
	});
	// router.$route.push({
	//     path: option.url,
	// 	query: option.data
	// });
}

Vue.prototype.$navTo = (url,params)=>{
	var page = getCurrentPages()[0]
	page.$Router.push({
	    path: url,
		query: params
	});
}
Vue.prototype.$replaceTo = (url,params)=>{
	var page = getCurrentPages()[0]
	page.$Router.replace({
	    path: url,
		query: params
	});
}
Vue.prototype.$navBack = (url,params)=>{
	var page = getCurrentPages()[0]
	page.$Router.back();
}
Vue.prototype.$getLocale = (url,params)=>{
	return uni.getLocale();
}
/**
 * 获取距离指定时间还有多少天
 * @param {String | Number | Date} dateTime ⽇期时间
 * @example
 * ```javascript
 * getDistanceSpecifiedTime('2019/02/02 02:02:00');
 * getDistanceSpecifiedTime(1549036800000);
 * getDistanceSpecifiedTime(new Date("2019/2/2 00:00:00"));
 * ```
 */
Vue.prototype.$getDistanceSpecifiedTime = (dateTime)=>{
	// 指定⽇期和时间
	var EndTime = new Date(dateTime);
	// 当前系统时间
	var NowTime = new Date();
	var t = EndTime.getTime() - NowTime.getTime();
	var d = Math.floor(t / 1000 / 60 / 60 / 24);
	var h = Math.floor(t / 1000 / 60 / 60 % 24);
	var m = Math.floor(t / 1000 / 60 % 60);
	var s = Math.floor(t / 1000 % 60);
	// var html = d + " 天" + h + " 时" + m + " 分" + s + " 秒";
	// console.log(html);
	return [d,h,m,s]
}


let i18nConfig = {
  locale: uni.getLocale(),
  messages
}

import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)
Vue.config.productionTip = false
Vue.prototype.$store = store
App.mpType = 'app'
const app = new Vue({
    store,
	i18n,
    ...App
})
//v1.3.5起 H5端 你应该去除原有的app.$mount();使用路由自带的渲染方式
// #ifdef H5
	RouterMount(app,'#app');
// #endif

// #ifndef H5
	app.$mount(); //为了兼容小程序及app端必须这样写才有效果
// #endif